package com.example.springbootproxy.jdk;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: wukangrong
 * @date: 2020/9/12 13:54
 * @description:
 */
@SuppressWarnings("unchecked")
@Slf4j
@Data
@AllArgsConstructor
public class MyHandler<T> implements InvocationHandler {

    private T t;


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("前置增强...");
        return method.invoke(t, args);
    }

    public <D> D getProxy(Class<D> clazz) {
        return (D) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, this);
    }
}
